<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="id ? '编辑月卡' :'添加月卡'" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="cardForm" label-width="100px" :model="cardForm" :rules="carRules">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="cardForm.personName" placeholder="请输入车主姓名" />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="cardForm.phoneNumber" placeholder="请输入联系方式" />
            </el-form-item>
            <el-form-item label="车牌号码" prop="carNumber">
              <el-input v-model="cardForm.carNumber" placeholder="请输入车牌号码" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="cardForm.carBrand" placeholder="请输入车辆品牌" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="payForm" label-width="100px" :model="payForm" :rules="payRules">
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
                v-model="payForm.payTime"
                type="daterange"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="payForm.paymentAmount" placeholder="请输入支付金额" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="payForm.paymentMethod" placeholder="请输入支付方式">
                <el-option
                  v-for="item in payMData"
                  :key="item.id"
                  :value="item.id"
                  :label="item.label"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="reset">重置</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCard, cardDetail, editCard } from '@/api/card'
export default {
  name: 'AddCard',
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          console.log(time)
          return time.getTime() < Date.now() - 86400 * 1000
        }
      },
      // 车辆信息
      cardForm: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      },
      // 车辆信息校验
      carRules: {
        personName: [{ required: true, message: '请输入车主姓名', trigger: 'blur' }],
        phoneNumber: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^(?:(?:\+|00)86)?1\d{10}$/, message: '请输入合法手机号码', trigger: 'blur' }
        ],
        carNumber: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' },
          { pattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-HJ-NP-Z][A-HJ-NP-Z0-9]{4,5}[A-HJ-NP-Z0-9挂学警港澳]$/, message: '请输入合法车牌号码', trigger: 'blur' }
        ],
        carBrand: [{ required: true, message: '请输入车辆品牌', trigger: 'blur' }]
      },
      // 缴费信息
      payForm: {
        payTime: '',
        paymentAmount: null,
        paymentMethod: ''

      },
      // 缴费校验
      payRules: {
        payTime: [{ required: true, message: '开始日期-结束日期', trigger: 'blur' }],
        paymentAmount: [{ required: true, message: '请输入支付金额', trigger: 'blur' }],
        paymentMethod: [{ required: true, message: '请输入支付方式', trigger: 'blur' }]
      },
      // 支付方式下拉列表信息
      payMData: [
        {
          id: 'Alipay',
          label: '支付宝'
        },
        {
          id: 'WeChat',
          label: '微信'
        },
        {
          id: 'Cash',
          label: '线下'
        }
      ]
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  created() {
    if (this.id) {
      this.getCardDetail()
    }
  },
  methods: {
    // 确定按钮
    submit() {
      // 兜底校验
      const promises = [this.$refs.cardForm.validate(), this.$refs.payForm.validate()]
      Promise.all(promises).then(async() => {
        try {
          const obj = { ...this.cardForm,
            ...this.payForm,
            cardStartDate: this.payForm.payTime[0],
            cardEndDate: this.payForm.payTime[1]
          }
          delete obj.payTime
          if (this.id) {
            // 编辑月卡
            await editCard(obj)
            this.$message.success('编辑成功')
          } else {
            // 添加月卡
            await addCard(obj)
            this.$message.success('添加成功')
          }
          this.$router.back()
        } catch (err) {
          if (err.response) {
            this.$message.error(err.response.data.msg)
          }
        }
      }).catch(() => {
        console.log('验证不通过')
      })
    },
    reset() {
      this.$refs.cardForm.resetFields()
      this.$refs.payForm.resetFields()
    },
    // 获取编辑月卡详情
    async getCardDetail() {
      try {
        const res = await cardDetail(this.id)
        console.log(res)
        const { personName, phoneNumber, carNumber, carBrand, paymentAmount, paymentMethod, cardStartDate, cardEndDate,
          carInfoId, rechargeId
        } = res.data
        // 车辆信息回显
        this.cardForm = {
          personName, phoneNumber, carNumber, carBrand,
          carInfoId
        }
        // 缴费信息回显
        this.payForm = {
          payTime: [cardStartDate, cardEndDate],
          paymentAmount,
          paymentMethod,
          rechargeId
        }
      } catch (err) {
        if (err.response) {
          this.$message.error(err.response.data.msg)
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-card {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }

      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;

        .el-form {
          display: flex;
          flex-wrap: wrap;

          .el-form-item {
            width: 50%;
          }
        }
      }
    }
    .preview{
      img{
        width: 100px;
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
</style>
